package Vista;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import Controlador.Sistema;
import DAO.DAOAutomovil;
import DAO.DAOModelo;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class frmAutomoviles extends JFrame {

	private JPanel contentPane;
	private JButton btnAlta;
	private JTable tblModelos;
	private JScrollPane jScrollPane1;
	private JButton btnModificar;
	private JButton btnEliminar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmAutomoviles frame = new frmAutomoviles();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public frmAutomoviles() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(0, 0, 579, 518);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		{
			btnEliminar = new JButton();
			contentPane.add(btnEliminar);
			btnEliminar.setText("Eliminar");
			btnEliminar.setBounds(251, 22, 115, 27);
			btnEliminar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					btnEliminarActionPerformed(evt);
				}
			});
		}
		{
			btnModificar = new JButton();
			contentPane.add(btnModificar);
			btnModificar.setText("Modificar");
			btnModificar.setBounds(140, 23, 106, 26);
			btnModificar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					btnModificarActionPerformed(evt);
				}
			});
		}
		{
			btnAlta = new JButton();
			contentPane.add(btnAlta);
			btnAlta.setText("Alta");
			btnAlta.setBounds(17, 23, 118, 26);
			btnAlta.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					btnAltaActionPerformed(evt);
				}
			});
		}
		{
			jScrollPane1 = new JScrollPane();
			contentPane.add(jScrollPane1);
			jScrollPane1.setBounds(17, 68, 536, 395);
			{
				TableModel tblModelosModel = 
					new DefaultTableModel(
							new String[][] { { "One", "Two" }, { "Three", "Four" } },
							new String[] { "Column 1", "Column 2" });
				tblModelos = new JTable();
				jScrollPane1.setViewportView(tblModelos);
				tblModelos.setModel(tblModelosModel);
				tblModelos.setPreferredSize(new java.awt.Dimension(533, 214));
				tblModelos.setBounds(0, 38, 262, 171);
			}
		}
		llenarGrilla();
	}

	private void btnAltaActionPerformed(ActionEvent evt) {
		frmAutomovil frm = new frmAutomovil("A", "");
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);
	}
	
	private void btnModificarActionPerformed(ActionEvent evt) {
		
		String patente = "";
		
		TableModel tablaM;
		tablaM = (TableModel) this.tblModelos.getModel();
		
		int registro = this.tblModelos.getSelectedRow();
		
		if (registro >= 0) {
			patente = (String) tablaM.getValueAt(registro, 0);;
		}
		else {
			return;			
		}
		frmAutomovil frm = new frmAutomovil("M", patente);
		
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);	}
	
	private void btnEliminarActionPerformed(ActionEvent evt) {
		
		String patente = "";
		
		TableModel tablaM;
		tablaM = (TableModel) this.tblModelos.getModel();
		
		int registro = this.tblModelos.getSelectedRow();
		
		if (registro >= 0) {
			patente = (String) tablaM.getValueAt(registro, 0);
		}
		else {
			return;
		}
		frmAutomovil frm = new frmAutomovil("E", patente);
		
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);	}


	
	private void llenarGrilla(){
		// Lleno la grilla de clientes

		ArrayList<DAOAutomovil> automoviles = Sistema.Getinstancia().ConsultarAutomoviles();
		
		DefaultTableModel tblModelosModel = new DefaultTableModel();
		
		tblModelosModel.addColumn("Patente");
		tblModelosModel.addColumn("Anio");
		tblModelosModel.addColumn("Kilometraje");
		
		
		for (DAOAutomovil daa : automoviles) {
			
			Vector<String> valores = new Vector<String>();
			
			valores.add(daa.getPatente().trim());
			valores.add(String.valueOf(daa.getAnio()));
			valores.add(String.valueOf(daa.getKilometraje()));
			
			tblModelosModel.addRow(valores);
		}

		tblModelos.setModel(tblModelosModel);
		
	}
		
}
